package cn.grab.common;

import cn.grab.fispservice.system.mapper.SysDictItemMapper;
import com.fontana.base.object.DictModel;
import com.fontana.redis.util.DictCacheHelper;
import com.fontana.redis.util.RedisTemplateUtil;
import com.fontana.util.tools.ConstantDictUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: OssTest
 * @Description: TODO
 * @version: v1.0.0
 * @author: cqf
 * @date: 2022/4/26 14:13
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootApplication(scanBasePackages = "cn.grab.fispservice")
@Slf4j
public class testDict {

    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    @Autowired
    private DictCacheHelper dictCacheHelper;

    @Test
    public void testDict() throws IOException {

        //系统字典
        Map<String, List<DictModel>> dictMap = sysDictItemMapper.getAllDictItems().stream().map(t->new DictModel(t.getDictCode(), t.getItemValue(), t.getItemText())).collect(Collectors.groupingBy(DictModel::getCode));

        //常量字典
        dictMap.putAll(ConstantDictUtil.getConstDictData());

        dictCacheHelper.refreshAllDict(dictMap);
        List<DictModel> list = dictCacheHelper.getDictItems("sys_user_status");
        list.forEach(System.out::println);

        DictModel dictModel = dictCacheHelper.getDictItemByValue("sys_user_status", "1");
        System.out.println("------------------------------------");
        System.out.println(dictModel);
        dictModel = dictCacheHelper.getDictItemByName("sys_user_status", "正常状态");
        System.out.println("------------------------------------");
        System.out.println(dictModel);
        dictCacheHelper.putDictItem(new DictModel("sys_user_status", "1", "失联"));
        list = dictCacheHelper.getDictItems("sys_user_status");
        System.out.println("------------------------------------");
        list.forEach(System.out::println);
        dictCacheHelper.putDictItem(new DictModel("sys_user_status", "3", "失忆"));
        list = dictCacheHelper.getDictItems("sys_user_status");
        System.out.println("------------------------------------");
        list.forEach(System.out::println);
        dictCacheHelper.removeDictItem(new DictModel("sys_user_status", "3", "失联"));
        list = dictCacheHelper.getDictItems("sys_user_status");
        System.out.println("------------------------------------");
        list.forEach(System.out::println);
        dictCacheHelper.removeDict("sys_user_status", "xxx", "tenant_status");
        List<DictModel> testlist = Arrays.asList(new DictModel("test_lala", "3", "失联"),new DictModel("test_lala", "1", "失联"));
        dictCacheHelper.putDict("test_lala", testlist);
        list = dictCacheHelper.getDictItems("test_lala");
        System.out.println("------------------------------------");
        list.forEach(System.out::println);
        List<DictModel> testlist2 = Arrays.asList(new DictModel("test_lala", "2", "胡婆婆"),new DictModel("test_lala", "4", "拉了"));
        dictCacheHelper.putDictIfAbsent("test_lala", testlist2);
        list = dictCacheHelper.getDictItems("test_lala");
        System.out.println("------------------------------------");
        list.forEach(System.out::println);
        dictCacheHelper.putDict("test_lala", testlist2);
        list = dictCacheHelper.getDictItems("test_lala");
        System.out.println("------------------------------------");
        list.forEach(System.out::println);

    }
}


